package top.abrsm.controller;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.abrsm.entity.AreaEntity;
import top.abrsm.service.IAreaService;
import top.abrsm.util.ResultBody;
import top.abrsm.util.SnowflakeIdWorker;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping ("area")
public class AreaController {

    @Resource
    private IAreaService iAreaService;


    @RequestMapping ("queryList")
    public ResultBody<AreaEntity> queryList (AreaEntity areaEntity) {
        List<AreaEntity> areaEntities = iAreaService.queryList ( areaEntity );
        return ResultBody.success ( areaEntities, iAreaService.count ( areaEntity ) );
    }

    @RequestMapping ("queryChild")
    public ResultBody<AreaEntity> queryChild (AreaEntity areaEntity) {
        List<AreaEntity> areaEntities = iAreaService.queryChild ( areaEntity );
        return ResultBody.success ( areaEntities, iAreaService.childCount ( areaEntity ) );
    }

    @RequestMapping ("saveSingle")
    public ResultBody<AreaEntity> saveSingle (AreaEntity areaEntity) {
        areaEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        areaEntity = iAreaService.saveSingle ( areaEntity );
        return ResultBody.success ( areaEntity );
    }

}
